package com.orange.off.cloud.cloudoff.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.app.MyApplication;
import com.orange.off.cloud.cloudoff.model.UploadResponse;
import com.orange.off.cloud.cloudoff.urls.AppUrls;
import com.orange.off.cloud.cloudoff.utils.JsonUtils;
import com.orange.off.cloud.cloudoff.utils.ProgressDlgUtil;
import com.orange.off.cloud.cloudoff.utils.SPUtil;
import com.orange.off.cloud.cloudoff.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * 楼盘资源--佣金奖励
 *
 * @author smile
 */
public class RealEstateCommissionFragment extends Fragment {

    @Bind(R.id.rt_noticeBack)
    RelativeLayout rtNoticeBack;
    @Bind(R.id.commission_type)
    TextView commissionType;
    @Bind(R.id.commission_visit)
    TextView commissionVisit;
    @Bind(R.id.commission_identify_chips)
    TextView commissionIdentifyChips;
    @Bind(R.id.commission_subscription)
    TextView commissionSubscription;
    @Bind(R.id.commission_signed)
    TextView commissionSigned;
    private String id = "";

    public static RealEstateCommissionFragment newInstance(String id) {
        RealEstateCommissionFragment fragment = new RealEstateCommissionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        getListData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.real_estate_commission_fragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }


    private void initViews() {
        id = getArguments().getString("id");

    }

    /**
     * 获取佣金奖励数据
     */
    void getListData() {
        ProgressDlgUtil.showProgressDlg("数据请求中,请稍后...", getActivity());
        RequestParams params = new RequestParams();
        params.addFormDataPart("aid", MyApplication.aid);
        params.addFormDataPart("xid", id);
        params.addFormDataPart("account", (String) SPUtil.get(getActivity(), "username", ""));
        params.addFormDataPart("type", "money");//获取类型报备规则：rule，佣金奖励：money
        HttpRequest.post(AppUrls.ruleOrCommission, params, new BaseHttpRequestCallback<UploadResponse>() {
            @Override
            public void onSuccess(UploadResponse uploadResponse) {
                super.onSuccess(uploadResponse);
                ProgressDlgUtil.stopProgressDlg();
                if (uploadResponse.getSuccess()) {
                    try {
                        JSONObject jsonObject = new JSONObject(uploadResponse.getData());
                        commissionType.setText(JsonUtils.getString(jsonObject, "renzheng", "") + "佣金奖励：");
                        commissionVisit.setText(JsonUtils.getString(jsonObject, "lf", "") + "元");
                        commissionIdentifyChips.setText(JsonUtils.getString(jsonObject, "rc", "") + "元");
                        commissionSubscription.setText(JsonUtils.getString(jsonObject, "rg", "") + "元");
                        commissionSigned.setText(JsonUtils.getString(jsonObject, "qy", "") + "元");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                } else {
                    ToastUtils.show(getActivity(), uploadResponse.getMessage(), Toast.LENGTH_SHORT);
                }

            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
                ProgressDlgUtil.stopProgressDlg();
            }

            @Override
            public void onProgress(int progress, long networkSpeed, boolean done) {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.rt_noticeBack)
    public void onClick() {
        getActivity().finish();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            getActivity().finish();
            return true;
        }
        return false;
    }

}
